#!/bin/sh -e

. CI/travis/lib.sh

install_sphinx() {
	if ! command_exists pip3 ; then
		pip install sphinx
		pip install sphinx-rtd-theme
	else
		pip3 install sphinx
		pip3 install sphinx-rtd-theme
	fi

}

handle_centos() {
	# needed for man2html and a few other popular tools
	yum search epel-release
	yum info epel-release
	yum -y install epel-release

	# FIXME: see about adding `libserialport-dev` from EPEL ; maybe libusb-1.0.0-devel...
	yum -y groupinstall 'Development Tools'
	yum -y install cmake libxml2-devel libusb1-devel libaio-devel \
		bzip2 gzip rpm rpm-build redhat-lsb-core

	# CENTOS 6 doesn't include python 3, it's too old.
	if is_centos_at_least_ver "7" ; then
		yum -y install python3-pip
	fi

	if is_centos_at_least_ver "8" ; then
		# On CentOS 8, avahi-devel & doxygen are in this repo; enable it
		yum config-manager --set-enabled PowerTools
	fi

	# CENTOS 6 will has issues with sphinx
	if is_centos_at_least_ver "7" ; then
		yum -y install doxygen man2html
		install_sphinx
	fi

	yum -y install avahi-devel
}

handle_centos_docker() {
	prepare_docker_image "centos:centos${OS_VERSION}"
}

handle_ubuntu_docker() {
	prepare_docker_image "ubuntu:${OS_VERSION}"
}

handle_default() {
	sudo apt-get -qq update
	sudo DEBIAN_FRONTEND=noninteractive apt-get install -y cmake graphviz \
		libaio-dev libavahi-client-dev \
		libavahi-common-dev libusb-1.0-0-dev libxml2-dev rpm tar \
		bzip2 gzip flex bison git lsb-release python3-pip

	if [ -n "${GH_DOC_TOKEN}" ] ; then
		sudo apt-get install -y doxygen man2html
		install_sphinx
	fi
	if [ `sudo apt-cache search libserialport-dev | wc -l` -gt 0 ] ; then
		sudo apt-get install -y libserialport-dev
	fi
}

OS_TYPE=${1:-default}

handle_${OS_TYPE}
